package ldh.im.fxgui.util;

import javafx.scene.image.Image;
import ldh.im.fxbase.emoji.ImageCache;
import ldh.im.fxbase.util.SoftMapUtil;

public class UserHeadImageUtil {

    public static Image getHeadImage(String url) {
        if (url == null) {
            String key = "/imgs/head.jpg";
            return getOrCreateImage(key);
        }
        if (!url.contains(":")) {
            String path = ImageCache.getInstance().getImagePath(url);
            if (path != null) {
                return ImageCache.getInstance().getImage(path);
            } else {
                return null;
            }
        }
        return getOrCreateImage(url);
    }

    private static Image getOrCreateImage(String url) {
        Image cacheImage = SoftMapUtil.getCache(url);
        if (cacheImage != null) return cacheImage;
        Image image = new Image(UiUtil.class.getResource(url).toExternalForm());
        SoftMapUtil.cache(url, image);
        return image;
    }
}
